//
//  WKForgetPasswordVM.m
//  WuKongUsernameLogin
//
//  Created by tt on 2023/9/14.
//

#import "WKForgetPasswordVM.h"

#import "WKMnemnonInputCell.h"
#import <WuKongUsernameLogin/WuKongUsernameLogin-Swift.h>

@interface WKForgetPasswordVM ()

@property(nonatomic,copy) NSString *username;
@property(nonatomic,copy) NSString *password;
@property(nonatomic,copy) NSString *mnemnons;

@end

@implementation WKForgetPasswordVM

- (NSArray<NSDictionary *> *)tableSectionMaps {
    __weak typeof(self) weakSelf = self;
    return @[
        @{
            @"height":@(40.0f),
            @"items":@[
                    @{
                        @"class":WKTextFieldItemModel.class,
                        @"placeholder":LLang(@"请填写账号"),
                        @"label": @"账号",
                        @"showBottomLine":@(true),
                        @"onChange": ^(NSString *value) {
                            weakSelf.username = value;
                        }
                    },
            ],
        },
        @{
            @"height":@(0.01f),
            @"items":@[
                    @{
                        @"class":WKTextFieldItemModel.class,
                        @"placeholder":LLang(@"请输入新密码"),
                        @"label": @"密码",
                        @"showBottomLine":@(true),
                        @"password":@(true),
                        @"onChange": ^(NSString *value) {
                            weakSelf.password = value;
                        }
                    },
            ],
        },
        @{
            @"height":@(40.0f),
            @"items":@[
                    @{
                        @"class":WKMnemnonInputModel.class,
                        @"onChange": ^(NSString *value) {
                            weakSelf.mnemnons = value;
                        }
                    },
            ],
        },
        @{
            @"height":@(80.0f),
            @"items":@[
                    @{
                        @"class":WKButtonItemModel2.class,
                        @"title": LLang(@"确定"),
                        @"height":@(40.0f),
                        @"onPressed":^{
                            [weakSelf onOk];
                        }
                    },
            ],
        },

    ];
}

-(void) onOk {
    UIView *topView = WKNavigationManager.shared.topViewController.view;
     if(!self.username || self.username.length == 0) {
         [topView showHUDWithHide:LLang(@"账号不能为空！")];
         return;
     }
     if(!self.password || self.password.length == 0) {
         [topView showHUDWithHide:LLang(@"密码不能为空！")];
         return;
     }
    if(!self.mnemnons || self.mnemnons.length == 0) {
        [topView showHUDWithHide:LLang(@"助记词不能为空！")];
        return;
    }
    NSString *uid = WKApp.shared.loginInfo.uid;
    NSString *privateKey = [WKWeb3Helper generatePrivatekey:self.mnemnons];
    if(!privateKey || [privateKey isEqualToString:@""]) {
        [topView showHUDWithHide:LLang(@"非法助记词，请检查")];
        return;
    }
    __weak typeof(self) weakSelf = self;
    [topView showHUD];
    [self getWeb3verifytext:self.username].then(^(NSDictionary *result){
        NSString *verifytext = result[@"verify_text"];
        NSData *verifyData = [verifytext dataUsingEncoding:NSUTF8StringEncoding];
        NSString *signText = [WKWeb3Helper signPersonalMessageWithPrivateKey:privateKey data:verifyData];
        
        [weakSelf updatePassword:verifytext signText:signText username:weakSelf.username newPassword:weakSelf.password].then(^{
            [topView switchHUDSuccess:LLang(@"修改成功")];
            [WKWeb3Helper createUserWallet:weakSelf.mnemnons uid:uid];
            [WKNavigationManager.shared popViewControllerAnimated:YES];
        }).catch(^(NSError *error){
            [topView switchHUDError:error.domain];
        });
        
    }).catch(^(NSError*error){
        [topView switchHUDError:error.domain];
    });
}


-(AnyPromise*) getWeb3verifytext:(NSString*)username {
    return [WKAPIClient.sharedClient GET:@"user/web3verifytext" parameters:@{@"username":username,@"type":@"password"}];
}


-(AnyPromise*) updatePassword:(NSString*)verifyText signText:(NSString*)signText username:(NSString*)username newPassword:(NSString*)password {
   return [WKAPIClient.sharedClient POST:@"user/pwdforget_web3" parameters:@{
        @"verify_text": verifyText,
        @"sign_text": signText,
        @"username": username,
        @"password": password,
    }];
}


@end
